var currentIndexTemp = 0;
var indexTemp = 0;
var keyId;
var MiningId;
var fdForeignId;
var	fdPlanYear;
var fdPlanMonth;

efform_onload = function ()
{	
	if(isTreeInited())
	{
		if(fdForeignId!=null&&fdForeignId!="")
		{
			
			var info = new EiInfo();
			info.set("fdForeignId",fdForeignId); 
			info.set("fdMiningId",MiningId); 
			EiCommunicator.send("ZMCH91", "query", info, ajax_callback1);
		} 
	}
	else
	{
		$('#ef_region_detail').hide();//隐藏从表区域
		$('#ef_region_inqu1').hide();
	}

	$('#inqu_status1-0-fdCreateDepartmentId').val($('#fdCreateDepartmentId').val());
	$('#inqu_status1-0-fdCreateDepartment').val($('#fdCreateDepartment').val());
	$('#inqu_status1-0-fdCreatorId').val($('#fdCreatorId').val());  
	departmentChanged();
}; 
//判断主表是否选中记录
function isTreeInited()
{
	  var grid = efgrid.getGridObject("ef_grid_r");
	  var rowslength = grid.getCheckedRowCount();
	if (rowslength == 0) 
		return false;
		
	else
		{
		   fdForeignId=grid.getCellValueByColumnName(row_index,"fdKeyId");
		   fdMiningId=grid.getCellValueByColumnName(row_index,"fdMiningId");
		   return true;
		}
		
}

var ajax_callback1 = {
	onSuccess : function(eiInfo) {
		efwindow.hide(); 
		var grid = efgrid.getGridObject("ef_grid_d");
		grid.refresh(eiInfo);
	},
	onFail : function(eMsg) {}
};

efgrid_onRowClicked = function( grid_id, row_index ){	
	if(grid_id == "ef_grid_r"){ 
		var grid = efgrid.getGridObject(grid_id);
		fdForeignId=grid.getCellValueByColumnName(row_index,"fdKeyId");
		fdMiningId=grid.getCellValueByColumnName(row_index,"fdMiningId");		
		var fdPlanStatus=grid.getCellValueByColumnName(row_index,"fdPlanStatus");
		keyId = grid.getCellValueByColumnName(row_index,"fdKeyId");
		MiningId = grid.getCellValueByColumnName(row_index,"fdMiningId");	
		fdPlanYear = grid.getCellValueByColumnName(row_index,"fdPlanYear");
		fdPlanMonth = grid.getCellValueByColumnName(row_index,"fdPlanMonth");

		fundiv(currentIndexTemp,indexTemp);
		//隐藏/显示从表按钮
//		if(fdPlanStatus!='草拟')
//			hideAndShowSubButtons(false);
//		else
//			hideAndShowSubButtons(true);
		var info = new EiInfo();
		info.set("fdForeignId",fdForeignId); 
		info.set("fdMiningId",fdMiningId); 
		$('#fdForeignId').val(fdForeignId);
		$('#fdMiningId').val(fdMiningId);
		$('#inqu_status1-0-fdForeignId').val(fdForeignId);
		$('#inqu_status1-0-fdMiningId').val(fdMiningId);
		EiCommunicator.send("ZMCH91", "query", info, ajax_callback);  
	}
	if(grid_id == "ef_grid_d"){ 
		var grid = efgrid.getGridObject(grid_id); 
		var selectedRow=grid.getRowData(row_index);
		for(var item in selectedRow){
			var colname=item;
			var colvalue=selectedRow[item];
			$('#ef_region_inqu1').find('#inqu_status1-0-'+colname).val(colvalue);
		} 
	}
}
var ajax_callback= {
	onSuccess: function(eiInfo) { 
		 efwindow.hide();
		 var grid = efgrid.getGridObject("ef_grid_d");
		 grid.refresh(eiInfo);
		 $('#ef_region_detail').show();
		 $('#ef_region_inqu1').show();
	},
	onFail:function(eMsg)
	{
		EFAlert("新增失败，原因："+eMsg); 
	}
}

/********************************************************************************
 ***********************************从表操作**************************************** 
 ********************************************************************************/
button_insert_onclick = function (){
	var isCheck = check();
  	if (isCheck == 0) {
        return;
    }
  	
	var info = getDetail("ef_region_inqu1","inqu_status1");
	
	if (null != info) { 
		EiCommunicator.send("ZMCH91", "insert", info, ajax_callbackconfirm);
	} 
}
button_update_onclick = function (){ 
	var isCheck = check();
  	if (isCheck == 0) {
        return;
    }

	var fdKeyId = $('#inqu_status1-0-fdKeyId').val();
	if (fdKeyId == "" || fdKeyId == null) {
		EFAlert("请选择一条记录进行修改！");
		return;
	}
	var info = getDetail("ef_region_inqu1","inqu_status1");
	if (null != info) {
		EiCommunicator.send("ZMCH91", "update", info, ajax_callbackconfirm);
	} 
}
button_delete_onclick = function (){
	var grid=isSelected("ef_grid_d");
	if (grid == 0)return;  
	EFConfirm('你确认要删除吗?', '确认框', function(r) {
		if(r){
			efgrid.submitForm( "ef_grid_d", "ZM","ZMCH91","delete",true);
		}
	}); 
}  
var ajax_callbackconfirm = {
	onSuccess : function(eiInfo) {
		efwindow.hide(); 
		var grid = efgrid.getGridObject("ef_grid_d");
		grid.refresh(eiInfo);
		clearTempData();
	}, 
	onFail:function(eMsg)
	{
		alert("确认失败，原因："+eMsg);
	}
}
function departmentChanged()
{ 
	  var departmentid=$('#inqu_status1-0-fdDepartmentId').val(); 
	  var info=_getEi();
	  var eiBlock=info.getBlock("cmes.cp.department");
	  for(var i=0;i<eiBlock.getRows().length;i++)
	  {
		  var _display = eiBlock.getCell(i,"orgName");
		  var _value = eiBlock.getCell(i,"orgCode");
		  if(_value==departmentid)
		  { 
			  $('#inqu_status1-0-fdDepartment').val(_display);
		  }
	  }
}
var hideAndShowSubButtons=function(status)
{
	if(!status)
	{
		efbutton.setButtonStatus("insert", false);
		efbutton.setButtonStatus("update", false);
		efbutton.setButtonStatus("delete", false);
	}else
	{
		efbutton.setButtonStatus("insert", true);
		efbutton.setButtonStatus("update", true);
		efbutton.setButtonStatus("delete", true);
	}
}

function fundiv(currentIndex,index){ 
	currentIndexTemp = currentIndex;
	indexTemp = index;
	if(index ==0)
	{  

//		var frame = frames[1];
//		frame.src = "DispatchAction.do?efFormEname=ZMCH90";
//		alert(frame.src);
//		efform.setStatus(0, "正在加载...");
//		EiCommunicator.send("ZMCH90", "query", info, ajax_chang_callback);
	}else if(index ==1)
	{ 				
		var frame = frames[0];	
		if( MiningId == null || MiningId == undefined || MiningId == ''){
			
			EFAlert("请先选择一条建造粉尘计划！"); 
//			var grid = efgrid.getGridObject("ef_grid_r");
//			efform.refresh(eiInfo);

		}else{
			frame.src = "DispatchAction.do?efFormEname=ZMCC00"+"&keyId="+keyId+"&MiningId="+MiningId+"&fdPlanYear="+fdPlanYear+"&fdPlanMonth="+fdPlanMonth+"&fdProblemType="+encodeURIComponent(encodeURIComponent("建造粉尘"));				
			efform.setStatus(0, "正在加载...");
		}
	} 
}


efgrid_onBeforeGridDisplay = function(grid_id) {
	if (grid_id == "ef_grid_r") {
		var grid = efgrid.getGridObject(grid_id);
		
		for (var i = 0; i < grid.getRowCount(); i++){	// 给数据列第4列操作列设置文本
			grid.setCellValue(i, 5, "查看详细" );
		}
	}
}
//efgrid_onRowClicked = function( grid_id, row_index ){
//	if(grid_id == "ef_grid_r"){ 
//		var grid = efgrid.getGridObject(grid_id);
//		keyId = grid.getCellValueByColumnName(row_index,"fdKeyId");
//		MiningId = grid.getCellValueByColumnName(row_index,"fdMiningId");		
//		fundiv(currentIndexTemp,indexTemp);
//		
//	}
//}; 

/**点击数据列的回调函数：当列类型为hyperlink或textbutton时点击有效 */
efgrid_onDataCellClick = function(grid_id, row_index, col_index, cell_value) {
	if (grid_id == "ef_grid_r") {		
		if (col_index == 5)
			efgrid_onRowClicked(grid_id, row_index);
//			alert("11111");		  
//		 parent.document.mainFrame.location = "DispatchAction.do?efFormEname=ZMCP07&keyId="+keyId;		
		 var childWindow = efform.openNewForm('ZMCHB001', "serviceName=ZMCHB001&methodName=query&keyId=" + keyId);
		  childWindow.focus();	 	  
	}
}
/**
 * 验收信息输入校验
 * @returns {Boolean}
 */
function check() {
	var fdValidateType = $("#inqu_status1-0-fdValidateType").val(); // 验收类型
	var fdValidateDate = $("#inqu_status1-0-fdValidateDate").val(); // 验收日期
	var fdValidatePeople = $("#inqu_status1-0-fdValidatePeople").val(); // 验收人

	var fdValidateDepartmentId = $("#inqu_status1-0-fdValidateDepartmentId")
			.val(); // 验收单位
	var fdValidateCondition = $("#inqu_status1-0-fdValidateCondition").val()
			.trim(); // 验收情况
	var fdValidateContent = $("#inqu_status1-0-fdValidateContent").val()
	.trim(); // 验收内容

	if (fdValidateType == "") {
		SCZXAlert("验收类型不能为空！");
		return false;
	}

	if (fdValidatePeople == "") {
		SCZXAlert("验收人不能为空！");
		return false;
	} else if (fdValidatePeople.getBytes() > 32
			|| fdValidatePeople.getBytes() < 1) {
		SCZXAlert("[验收人]请输入1~32个字符！");
		return false;
	}
	if (fdValidateDepartmentId == "") {
		SCZXAlert("验收单位不能为空！");
		return false;
	}

	if (fdValidateCondition == "") {
		SCZXAlert("验收情况不能为空！");
		return false;
	} else if (fdValidateCondition.getBytes() > 100
			|| fdValidateCondition.getBytes() < 1) {
		SCZXAlert("[验收情况]请输入1~100个字符！");
		return false;
	}

	if (fdValidateContent.getBytes() > 500) {
		SCZXAlert("[验收内容]不能超过500个字符！");
		return false;
	}

	if (fdValidateDate == "" || fdValidateDate == 0) {
		SCZXAlert("验收日期不能为空！");
		return false;
	} else {
		if (getDateValue(fdValidateDate + " 00:00:00") > getDateValue(getNowDate() + " 00:00:00")) {
			SCZXAlert("验收时间不能晚于当前时间，请重新输入！");
			return false;
		}
	}
	return true;
}

String.prototype.getBytes = function() {      
    var cArr = this.match(/[^\x00-\xff]/ig);      
    return this.length + (cArr == null ? 0 : cArr.length);   
      
}  

String.prototype.trim=function(){
    return this.replace(/(^\s*)|(\s*$)/g, "");
 }
/*
根据传入的String类型的date获取该日期对应的毫秒值，传入的字符串格式为：yyyy-MM-dd HH:mm:ss
*/
function getDateValue(dateStr) {
   var tt = dateStr.split(" ");
   var ttt1 = tt[0].split("-");
   var ttt2 = tt[1].split(":");
   var date1 = new Date(ttt1[0], ttt1[1], ttt1[2], ttt2[0], ttt2[1], ttt2[2]);
   var m1 = date1.getTime();
   return m1;
}
/**
 * 操作完成后清空该region内的各数据项的值
 */
function clearTempData(){
	$('#inqu_status1-0-fdKeyId').val('');
	$('#inqu_status1-0-fdValidateType').val('');
	$('#inqu_status1-0-fdValidateDate').val('');
	$('#inqu_status1-0-fdValidatePeople').val('');
	$('#inqu_status1-0-fdValidateDepartmentId').val('');
	$('#inqu_status1-0-fdValidateCondition').val('');
	$('#inqu_status1-0-fdValidateContent').val('');
}
